package com.google.gwt.it2tool.server;

import java.util.List;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.it2tool.client.CustomFactoryOrder;
import com.google.gwt.it2tool.client.Factory;
import com.google.gwt.it2tool.client.FactoryService;
import com.google.gwt.it2tool.client.NotLoggedInException;
import com.google.gwt.it2tool.client.Production;
import com.google.gwt.it2tool.client.ProductionPrototype;
import com.google.gwt.it2tool.client.Recipe;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class FactoryServiceImpl extends RemoteServiceServlet implements FactoryService {

	private DataStorageAdapter dataStorage = new DataStorageAdapter();


	public void addFactory(Factory factory) throws NotLoggedInException {
		checkLoggedIn();
		dataStorage.addFactory(factory);
	}

	public void removeFactory(Factory factory) throws NotLoggedInException {
		checkLoggedIn();
		dataStorage.removeFactory(factory);
	}
	
	public void addProduction(Production prod) throws NotLoggedInException {
		checkLoggedIn();
		dataStorage.addProduction(prod);
	}

	public void removeProduction(Production prod) throws NotLoggedInException {
		checkLoggedIn();
		dataStorage.removeProduction(prod);
	}

	public List<Factory> getFactories() throws NotLoggedInException{
		checkLoggedIn();
		return dataStorage.getSortedFactories();
	}
	
	public void storeMassProductionLvl(int lvl) throws NotLoggedInException {
		checkLoggedIn();
		dataStorage.storeMassProductionLvl(lvl);
	}
	public Integer getMassProductionLvl() throws NotLoggedInException {
		checkLoggedIn();
		return dataStorage.getMassProductionLvl();
	}	

	public void storeRecipe(Recipe rec) throws NotLoggedInException {
		checkLoggedIn();
		dataStorage.storeRecipe(rec);
	}

	public List<Recipe> getRecipies() throws NotLoggedInException {
		checkLoggedIn();
		return dataStorage.getRecipies();
	}
	
	@Override
	public List<ProductionPrototype> getProductionPrototypes()
			throws NotLoggedInException {
		checkLoggedIn();
		return dataStorage.getProductionPrototypes();
	}

	@Override
	public ProductionPrototype getProductionPrototype(String name)
			throws NotLoggedInException {
		checkLoggedIn();
		return dataStorage.getProductionPrototype(name);
	}
	
	@Override
	public void storeCustomFactoryOrder(CustomFactoryOrder order)
			throws NotLoggedInException {
		checkLoggedIn();
		dataStorage.storeCustomFactoryOrder(order);
		
	}

	@Override
	public CustomFactoryOrder getCustomFactoryOrder()
			throws NotLoggedInException {
		checkLoggedIn();
		return dataStorage.getCustomFactoryOrder();
	}

	private void checkLoggedIn() throws NotLoggedInException {
		if (getUser() == null) {
			throw new NotLoggedInException("Not logged in.");
		}
	}

	private User getUser() {
		UserService userService = UserServiceFactory.getUserService();
		return userService.getCurrentUser();
	}




}